	$(document).ready( function() {
		$('#gameDate').val(new Date().toJSON().substring(0,10));
		// Inserts today's date as default value for Game Date.
	});

	function submitNewGame(i_TeamID) {
		$(".inputError").attr('hidden', true);
		if (validateNewGame()) {
			sendToServer(i_TeamID);
		}
	}
	
	function sendToServer(i_TeamID){
		$.ajax({
			url : "servletGame",
			type : "POST",
			data : {
				action : "insert",
				teamID : i_TeamID,
				gameDate : $("#gameDate").val(),
				gameBuyIn : $("#gameBuyIn").val(),
				gameRebuy : $("#gameRebuy").val(),
				gameRemark : escapeStr($("#gameRemark").val())
			},
			success : afterSubmit
		});
	}
	
	function afterSubmit(data, status){
		if (data.report == "success"){
			$("#submitResult").html("Saved game to database! Going to games page now...");
			setTimeout(function(){displayGame(data.gameID);},3000);
		}
		else {
			$("#submitResult").html("Something went wrong! Couldn't save new game. :(");
		}
	}

	function validateNewGame() {
		var isValid = true;
		if ($("#gameDate").val() == "") {
			isValid = false;
			$("#dateError").attr('hidden', false);
		}
		
		if ($("#gameBuyIn").val() == "") {
			isValid = false;
			$("#buyInError").attr('hidden', false);
		}
		
		if ($("#gameRebuy").val() == "") {
			isValid = false;
			$("#rebuyError").attr('hidden', false);
		}
		return isValid;
	}
	
	function displayGame(i_GameID){
		$.ajax({
			url : "ShowSingleGameRecords.jsp",
			type : "POST",
			data : {
				gameID : i_GameID
			},
			success : function(data, status) {
				changeDivMainContainer(data);
			}
		});
	}